/**
 * Created by Jason_PC on 2017/8/24.
 */
// mongoose init
var url = 'mongodb://127.0.0.1:27017/webIM'; // 连接mongodb的url
var mongoose = require('mongoose');//加载mongoose模块
mongoose.connect(url);

// express init
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
// 给app配置bodyParser中间件
// 通过如下配置再路由种处理request时，可以直接获得post请求的body部分
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var http = require('http').Server(app);

var path = require('path');
app.use(express.static(path.join(__dirname, '/')));


// API  的路由定义
// =============================================================================
var router = express.Router();              // 获取express Router的实例

// 任何路由的每次request都执行
router.use(function (req, res, next) {
    // 打印
    console.log('Something is happening.');
    next(); // 在这里会将request交给下一个中间件，如果这个中间件后面没有其他中间件，请求会交给匹配的路由作处理
});
// 允许跨域访问
app.all('*', function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' cjatech')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});

// 用这个路由来做简单的测试(用get动词访问 http://localhost:8080/api)
router.get('/', function (req, res) {
    res.json({message: 'Welcome to our api!'});
});

// 这里可以配置更多的路由



var GiftModel = require('./models/gift');
// api/gifts 获取礼物信息
router.route('/gifts').get(function (req, res) {
    GiftModel.find({project_code:req.query.project_code,is_del:false}, {}, function (err, docs) {
        res.json(docs);
    })
}).post(function(req,res){
    var body = req.body;
    var gift = new GiftModel();
    gift.project_code =body.project_code;
    gift.gid=body.gid;
    gift.name = body.name;
    gift.price = body.price;
    gift.price_label = body.price_label;
    gift.img.effect = body.effect;
    gift.img.ext = body.ext;
    gift.img.icon = body.icon;
    gift.save(function (err) {
        if(err){
            res.send(err);
        }else{
         res.json({message:'gift created'})
        }
    })
}).put(function(req,res){
    GiftModel.findById(req.query._id,function (err, gift) {
        if(req.body.name){
            gift.name = req.body.name;
        }
        if(req.body.gid){
            gift.gid = req.body.gid;
        }
        if(req.body.project_code){
            gift.project_code = req.body.project_code;
        }
        if(req.body.price){
            gift.price = req.body.price;
        }
        if(req.body.price_label){
            gift.price_label = req.body.price_label;
        }
        if(req.body.effect){
            gift.img.effect = req.body.effect;
        }
        if(req.body.ext){
            gift.img.ext = req.body.ext;
        }
        if(req.body.icon){
            gift.img.icon = req.body.icon;
        }
        gift.save(function (err) {
            if(err){
                res.send(err);
            }
            res.json({message:'updated'})
        })
    })
});
router.route('/gift/del').get(function(req,res){
    GiftModel.findById(req.query._id,function (err, gift) {
        gift.is_del = true;
        gift.save(function (err) {
            if(err){
                res.send(err);
            }
            res.json({message:'gift deleted'})
        })
    })
});
var EmojiModel = require('./models/emoji');
// api/emojis 获取表情信息
router.route('/emojis').get(function (req, res) {
    EmojiModel.find({project_code:req.query.project_code,is_del:false}, {}, function (err, docs) {
        res.json(docs);
    })
}).post(function (req, res) {
    var emoji = new EmojiModel();
    emoji.is_del = false;
    emoji.name = req.body.name;
    emoji.png = req.body.png;
    emoji.project_code = req.body.project_code;
    emoji.save(function (err) {
        if (err)
            res.send(err);

        res.json({message: 'emoji created'});
    })
}).put(function (req, res) {
    console.log(req.params);
    EmojiModel.findById(req.query._id,function (err, emoji) {
        if(req.body.name){
            emoji.name = req.body.name;
        }
        if(req.body.png){
            emoji.png = req.body.png;
        }
        if(req.body.project_code){
            emoji.project_code = req.body.project_code;
        }
        emoji.__v=0;
        emoji.save(function (err) {
            if(err){
                res.send(err);
            }
            res.json({message:'updated'})
        })
    })
});
router.route('/emojis/del').get(function(req,res){
    EmojiModel.findById(req.query._id,function (err, emoji) {
        emoji.is_del = true;
        emoji.save(function (err) {
            if(err){
                res.send(err);
            }
            res.json({message:'emoji deleted'})
        })
    })
});



/*
 var gift = new GiftModel({
 gid: '4',
 name: '只手遮天',
 price: 250,
 price_label: '250元',
 img: {
 effect: 'http://cdn.res.cjatech.com/upload/test.cjatech.com/img/636391708319580794.png',
 ext: 'http://cdn.res.cjatech.com/upload/test.cjatech.com/img/636391708319580794.png',
 icon: 'http://cdn.res.cjatech.com/upload/test.cjatech.com/img/636391708319580794.png',
 }
 });
 gift.save(function (err) {
 if(err){
 console.log('found an error')
 }
 })*/
// 注册我们的路由 -------------------------------
// 所有的路由都会加上前缀 /api
app.use('/api', router);
http.listen(8001, function () {
    console.log('listening on * 8001')
});